package com.yxhl.zoume;

import android.content.Context;
import android.support.multidex.MultiDexApplication;

import com.facebook.stetho.Stetho;
import com.squareup.leakcanary.RefWatcher;
import com.umeng.message.MsgConstant;
import com.umeng.message.PushAgent;
import com.umeng.socialize.PlatformConfig;
import com.yxhl.zoume.core.func.push.ZMUmengMessageHandler;
import com.yxhl.zoume.core.func.push.ZMUmengNotificationClickHandler;
import com.yxhl.zoume.di.component.base.AppComponent;
import com.yxhl.zoume.di.component.base.DaggerAppComponent;
import com.yxhl.zoume.di.module.AppModule;
import com.yxhl.zoume.utils.LOG;


/**
 * Created by Xboy on 16/2/24.
 */
public class ZouMeApplication extends MultiDexApplication {

    private static final String TAG = "ZouMeApplication";

    public static ZouMeApplication application;
    public boolean isFirstTimeCheckedAppUpdate;
    // 内存泄漏监控
    private RefWatcher refWatcher;

    private AppComponent appComponent;

    public static RefWatcher getRefWatcher(Context context) {
        ZouMeApplication application = (ZouMeApplication) context.getApplicationContext();
        return application.refWatcher;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        LOG.setIsRelease(!BuildConfig.LOG_DEBUG);

        application = this;
        isFirstTimeCheckedAppUpdate = true;
        LOG.e(TAG, "onCreate");

        refWatcher = installLeakCanary();
        Stetho.initializeWithDefaults(this);

        // 初始化AppComponent
        initializeInjector();

        // 第三方类库配置
        setupUMShare();
        initUMPush();
    }

    /**
     * 内存泄漏 调试版本和线上版本的切换
     *
     * @return
     */
    protected RefWatcher installLeakCanary() {
        //LeakCanary.install(this);
        return RefWatcher.DISABLED;
    }


    /**
     * author vhawk
     * 初始化友盟推送点击事件配置与消息接受事件配置
     */
    private void initUMPush() {
        PushAgent mPushAgent = PushAgent.getInstance(this);
        mPushAgent.setDebugMode(true);
        mPushAgent.setMessageHandler(new ZMUmengMessageHandler());
        mPushAgent.setNotificationClickHandler(new ZMUmengNotificationClickHandler());

        mPushAgent.setNotificationPlaySound(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE); //声音
        mPushAgent.setNotificationPlayLights(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);//呼吸灯
        mPushAgent.setNotificationPlayVibrate(MsgConstant.NOTIFICATION_PLAY_SDK_ENABLE);//振动
    }

    /**
     * 初始化注入器
     */
    private void initializeInjector() {
        appComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
    }

    public AppComponent getAppComponent() {
        return appComponent;
    }

    /**
     * 友盟分享
     */
    private void setupUMShare() {
        PlatformConfig.setWeixin(ZMConfig.WX_APPID, ZMConfig.WX_APPSECRET);
    }
}
